package cn.chenfei.jfx.login;

import cn.chenfei.jfx.App;
import cn.chenfei.jfx.common.JFUtils;
import cn.chenfei.jfx.common.QrUtil;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;

import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import javafx.scene.layout.*;

import org.kordamp.ikonli.antdesignicons.AntDesignIconsFilled;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

import java.io.*;

import java.util.Random;

/**
 * @author ChenFei
 * @date 2022年4月8日
 */
public class Login_1 extends StackPane {

    //这个根据实际情况需要做处理,这里只是临时写的
    private static String FILE_ROOT;

    static {
        try {
            FILE_ROOT = new File("").getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private HBox container = new HBox();
    private VBox leftContainer = new VBox();
    private VBox rightContainer = new VBox();
    private Label message = new Label();

    {
        this.getChildren().add(0, container);
        rightContainer.setAlignment(Pos.TOP_CENTER);
        message.getStyleClass().add("message");
        //拖动
        JFUtils.dragCurrentWindow(container);
    }

    private Button close = new Button();
    private VBox main = new VBox();

    private StackPane stackPane = new StackPane();
    //用户名装饰
    private HBox userHBox = new HBox();
    private Label userLabel = new Label();
    private TextField userField = new TextField();


    {
        userHBox.getChildren().addAll(userLabel, userField);
        userHBox.setHgrow(userField, Priority.ALWAYS);
        userHBox.getStyleClass().add("user-hbox");
        userLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.USER));
        userLabel.getStyleClass().add("user-label");
        userField.setPromptText("用户名");
        userField.getStyleClass().add("user-field");
    }

    //密码框装饰
    private HBox pswHBox = new HBox();
    private Label pswLabel = new Label();
    private PasswordField pswField = new PasswordField();

    {
        pswHBox.getChildren().addAll(pswLabel, pswField);
        userHBox.setHgrow(pswField, Priority.ALWAYS);
        pswHBox.getStyleClass().add("psw-hbox");
        pswLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.KEY));
        pswLabel.getStyleClass().add("psw-label");
        pswField.getStyleClass().add("psw-field");
        pswField.setPromptText("密码");
    }

    private StackPane bb = new StackPane();
    private CheckBox remember = new CheckBox("记住我");
    private Button login = new Button("登录");

    {
        remember.setGraphic(new FontIcon(AntDesignIconsFilled.CHECK_CIRCLE));
        remember.getStyleClass().add("remember");
        remember.setSelected(true);
        login.getStyleClass().add("login");
        login.setGraphic(new FontIcon(AntDesignIconsOutlined.LOGIN));
        bb.getChildren().addAll(remember, login);
        bb.setAlignment(remember, Pos.CENTER_LEFT);
        bb.setAlignment(login, Pos.CENTER_RIGHT);
    }

    private StackPane sp = new StackPane();
    private Label register = new Label("没账号，注册一个");
    private Label forget = new Label("忘记密码？");

    {
        register.getStyleClass().add("register");
        forget.getStyleClass().add("forget");
        sp.getChildren().addAll(register, forget);
        sp.setAlignment(register, Pos.CENTER_LEFT);
        sp.setAlignment(forget, Pos.CENTER_RIGHT);
    }

    private HBox qt = new HBox();
    private Separator ls = new Separator();
    private Label qtLabel = new Label("其他方式登录");
    private Separator rs = new Separator();

    {
        qtLabel.getStyleClass().add("qt-label");
        qt.getChildren().addAll(ls, qtLabel, rs);
        qt.setHgrow(ls, Priority.ALWAYS);
        qt.setHgrow(rs, Priority.ALWAYS);
        qt.setAlignment(Pos.CENTER);
    }

    private Button zfbButton = new Button("支付宝");
    private Button wxButton = new Button("微信");
    private Button wbButton = new Button("微博");

    {
        zfbButton.setGraphic(new FontIcon(AntDesignIconsOutlined.ALIPAY_CIRCLE));
        wxButton.setGraphic(new FontIcon(AntDesignIconsOutlined.WECHAT));
        wbButton.setGraphic(new FontIcon(AntDesignIconsOutlined.WEIBO));
        zfbButton.getStyleClass().add("zfb-but");
        wxButton.getStyleClass().add("wx-but");
        wbButton.getStyleClass().add("wb-but");
    }

    private VBox other = new VBox();
    private Label otherLabel = new Label();
    private Pane otherPane = new Pane();
    private ImageView imageView = new ImageView();

    {
        other.getStyleClass().add("other");
        otherLabel.getStyleClass().add("other-label");
        otherPane.getStyleClass().add("other-pane");
        other.getChildren().addAll(otherLabel, imageView);
        //点击遮罩层关闭
        other.setOnMouseClicked(event -> this.getChildren().remove(1));
    }

    public Login_1() {
        /**
         * 加载css
         */
        this.getStylesheets().add(App.class.getResource("css/Login_1.css").toExternalForm());
        /**
         * 布局
         */
        main.getChildren().addAll(userHBox, pswHBox, bb, sp, qt, zfbButton, wxButton, wbButton);
        stackPane.getChildren().add(main);
        close.setGraphic(FontIcon.of(AntDesignIconsOutlined.CLOSE));
        leftContainer.getChildren().addAll(close, stackPane);
        container.getChildren().addAll(leftContainer, rightContainer);
        /**
         * 添加class
         */
        container.getStyleClass().add("container");
        leftContainer.getStyleClass().add("left-container");
        rightContainer.getStyleClass().add("right-container");
        close.getStyleClass().add("close");
        main.getStyleClass().add("main");
        /**
         * 设置事件
         */
        setEvent();
    }

    private void setEvent() {
        close.setOnMouseClicked(event -> JFUtils.closeCurrentWindow(event.getSource()));
        //输入框选中事件绑定
        userField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "#409EFF;" : "#c6e2ff;";
            userHBox.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });
        pswField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "#409EFF;" : "#c6e2ff;";
            pswHBox.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });
        //登录点击事件
        login.setOnMouseClicked(event -> {
            event.consume();
            if (Cursor.WAIT.equals(login.getCursor())) {
                return;
            }
            String username = userField.getText();
            String password = pswField.getText();
            boolean reme = remember.isSelected();
            System.out.println(String.format("username: %s ; password: %s ; reme : %s ;", username, password, reme));
            if (username.trim().equals("")) {
                showMessage("用户名不能为空");
                return;
            }
            if (password.trim().equals("")) {
                showMessage("密码不能为空");
                return;
            }
            login.setCursor(Cursor.WAIT);
        });
        //注册
        EventHandler eventUrl = (EventHandler<Event>) e -> new App().getHostServices().showDocument("https://openjfx.cn/");
        register.setOnMouseClicked(eventUrl);
        //忘记密码
        forget.setOnMouseClicked(eventUrl);
        //其他方式登录
        EventHandler eventLogin = (EventHandler<Event>) e -> {
            //内容
            Button button = (Button) e.getSource();
            FontIcon fontIcon = (FontIcon) button.getGraphic();
            otherLabel.setGraphic(new FontIcon(fontIcon.getIconCode()));
            String filePath = FILE_ROOT + "/qrCode.png";
            QrUtil.writeToStream(filePath, 200, 200, new Random().nextInt() + "");
            imageView.setImage(new Image("file:" + filePath));
            this.getChildren().add(1, other);
        };
        zfbButton.setOnMouseClicked(eventLogin);
        wxButton.setOnMouseClicked(eventLogin);
        wbButton.setOnMouseClicked(eventLogin);

    }

    /**
     * 登录提示
     *
     * @param mess
     */
    private void showMessage(String mess) {
        message.setText(mess);
        if (rightContainer.getChildren().isEmpty()) {
            rightContainer.getChildren().add(message);
        } else {
            rightContainer.getChildren().set(0, message);
        }
        //3秒后关闭
        new Thread(() -> {
            try {
                Thread.sleep(3000);
                Platform.runLater(() -> rightContainer.getChildren().clear());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

}
